package ert;

import robocode.control.BattlefieldSpecification;
import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests if RiceMusubi moves from the corner into the center.
 * 
 * @author Emerson Tabucol Adapted code from TestDaCruzerMovement.java
 * 
 */
public class TestRiceMusubiCornerToCenter extends RobotTestBed {
  /** True if the robot visited any corner during the test case. */
  boolean visitedCorner = false;
  /** True if the robot visited any corner during the test case. */
  boolean onCenter = false;

  /**
   * Specifies that SittingDuck and DaCruzer are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,ert.RiceMusubi";
  }

  /**
   * This test runs for 10 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * After each turn, check to see if we're at a corner and then in the center.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    BattlefieldSpecification b = new BattlefieldSpecification();
    double xBattleField = b.getWidth();
    double yBattleField = b.getHeight();
    double xCenter = xBattleField / 2;
    double yCenter = yBattleField / 2;

    // Checks if the robot is in any of the corner
    if (((robot.getX() <= 100) && (robot.getY() <= 100))
        || ((robot.getX() > xBattleField - 100) && (robot.getY() <= 100))
        || ((robot.getX() <= 100) && (robot.getY() > yBattleField - 100))
        || ((robot.getX() > xBattleField - 100) && (robot.getY() > yBattleField - 100))) {
      // True if it went into any of the corners
      visitedCorner = true;
    }

    // Checks if the robot went near the center
    if (((robot.getX() < xCenter + 100) && (robot.getX() > xCenter - 100))
        && ((robot.getY() < yCenter + 100) && (robot.getY() > yCenter - 100))
        && !visitedCorner) {
      // True if it's near the center
      onCenter = true;

    }

  }

  /**
   * After the battle, check to see that the robot went into the center.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Check onCenter", onCenter);

  }

}
